home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / vidhandl / fontsel.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-03  |  8.9 KB  |  375 lines

  1. /* FONTSEL - Selects temporarily another DOS character font         */
  2. /* Freeware version                                                 */
  3. /* By Marcio Afonso Arimura Fialho                                  */
  4. /* http://pessoal.iconet.com.br/jlfialho                            */
  5. /* e-mail: jlfialho@iconet.com.br or (alternate) jlfialho@yahoo.com */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <crt.h>
  11. #include <ctype.h>
  12.  
  13. #define EGA 3 //EGA color video adapters
  14. #define VGA 9 //VGA/SVGA+ color video adapters
  15.  
  16. #define VIDEOADAPTER VGA
  17.     //if your adapter is EGA, replace VIDEOADAPTER
  18.  
  19. int changechar_rows=25;
  20.  
  21. int chrup(int chr) //upcases input letter
  22.  {
  23.     if (chr>0x60 && chr<0x7b)
  24.         chr-=0x20;
  25.     return chr;
  26.  }
  27.  
  28. int asctoi (int *rtn, char *s, unsigned radix)
  29. //converts a string (in base "radix") to integer
  30. //radix must be between 2 and 36, inclusive.
  31.  {
  32.     unsigned register a0; //converted value
  33.     unsigned register a1,a2; //auxiliar variables
  34.     a0=0;
  35.     if (radix<2 || radix >36)
  36.      {
  37.         *rtn=a0;
  38.         return -3; //radix value is incorrect
  39.      }
  40.     while (*s)
  41.      {
  42.         a1=toupper (*s);
  43.         if(a1>='0' && a1<='9')
  44.             a1-='0';
  45.         else if(a1>='A' && a1<='Z')
  46.             a1-='7';
  47.         else
  48.          {
  49.             *rtn=a0;
  50.             return -1; //unexpected character
  51.          }
  52.         if (a1>=radix)
  53.          {
  54.             *rtn=a0;
  55.             return -2; //digit is out of range
  56.          }
  57.         a2=a0;
  58.         a0*=radix;
  59.         a0+=a1;
  60.         if ((a0/radix)!=a2)
  61.          {
  62.             *rtn=a0;
  63.             return -4; //overflow
  64.          }
  65.         s++;
  66.      }
  67.     *rtn=a0;
  68.     return 0;
  69.  }
  70. // rtn is the converted value (until the point the error occured).
  71. // hextoi returns 0 if sucessful. On error returns:
  72. // -1 -> unexpected character in "s"
  73. // -2 -> digit is out of range given by radix
  74. // -3 -> radix value is incorrect
  75. // -4 -> overflow
  76.  
  77. int hextobin (int c) //converts an hexadecimal character to integer
  78.  {
  79.     c=chrup (c);
  80.     if(c>='0' && c<='9')
  81.         return c-'0';
  82.     if(c>='A' && c<='F')
  83.         return c-('A'-10);
  84.     return -1; //error code
  85.  }
  86.  
  87. #include "readname.cpp"
  88.  
  89. unsigned char *buffer;
  90.  
  91. void main (int n, char *ent[4])
  92.  {
  93.     int c0,c1;
  94.     int cm;
  95.     int a0,a1;
  96.     int flags=0;
  97.     int progflag=0;
  98.     int newmode; //new video mode
  99.     long src_offset=0; //start loading font from offset in font file
  100.     int startpos=0; //index of the first character to be replaced
  101.     int endpos=255; //index of the last character to be replaced
  102.     FILE *source;
  103.     char *path;
  104.     int cont;
  105.     char licos[128];
  106.     char temp [128];
  107.     char *errormsg="\nType\tFONTSEL /?\t for help\n";
  108.     char c;
  109.  
  110.     printf ("FONTSEL ver 1.6 - DOS FONT SELECTOR - By Márcio Afonso Arimura Fialho\n");
  111.     if (n<2)
  112.      {
  113.         printf (errormsg);
  114.         return;
  115.      }
  116.  
  117.     changechar_height=16;
  118.  
  119.     for (cm=1;cm<n;cm++)
  120.      {
  121.         if (*ent[cm]=='/')
  122.          {
  123.             c=chrup(*(ent[cm]+1));
  124.             switch (c)
  125.              {
  126.                 case 'I': flags|=0x01; break;
  127.                 case '8': flags|=0x0a; break;
  128.                 case '9': flags|=0x08; break;
  129.                 case 'A':
  130.                 case 'D': flags|=0x10; break;
  131.                 case 'R': flags|=0x20; break;
  132.                 case 'M':
  133.                     flags|=0x40;
  134.                     a0=strlen(ent[cm]);
  135.                     if (a0>2)
  136.                      {
  137.                         if (asctoi(&newmode,ent[cm]+2,16))
  138.                             goto m_error;
  139.                         if (newmode&0xFF00) //if (unsigned)a1>256
  140.                             goto m_error;
  141.                      }
  142.                      else
  143.                      {
  144.                       m_error:
  145.                         printf ("\nERROR: Wrong mode value. Expected => (/M0 - /MFF)hex%s",errormsg);
  146.                         return;
  147.                      }
  148.                  break;
  149.                 case 'G': flags|=0x080; break;
  150.                 case 'L':
  151.                     flags|=0x100;
  152.                     a0=strlen(ent[cm]);
  153.                     if (a0>2)
  154.                      {
  155.                         if (asctoi(&changechar_rows,ent[cm]+2,10))
  156.                             goto l_error;
  157.                         if (changechar_rows&0xFF00)
  158.                             goto l_error;
  159.                      }
  160.                      else
  161.                      {
  162.                       l_error:
  163.                         printf ("\nERROR: Wrong input value for rows. Expected => (/L0 - /L255)%s",errormsg);
  164.                         return;
  165.                      }
  166.                     break;
  167.                 case 'H':
  168.                     a0=strlen(ent[cm]);
  169.                     if (a0==2)
  170.                      {
  171.                         printf ("\nWarning: Box height assumed to be 16 (default)");
  172.                         break;
  173.                      }
  174.  
  175.                     if (asctoi(&changechar_height,ent[cm]+2,10))
  176.                         goto h_error;
  177.                     if (changechar_height&0xFF00)
  178.                         goto h_error;
  179.                     break;
  180.                   h_error:
  181.                     printf ("\nERROR: Wrong box height. Expected => (/H0 - /H255)%s",errormsg);
  182.                     return;
  183.                 case 'O':
  184.                     a0=strlen(ent[cm]);
  185.                     if (a0==2)
  186.                      {
  187.                         printf ("\nWarning: Offset assumed to be 0 (default).");
  188.                         break;
  189.                      }
  190.                     for (c0=2;c0<a0;c0++)
  191.                      {
  192.                         a1=*(ent[cm]+c0);
  193.                         if (a1<'0' || a1>'9')
  194.                             goto o_error;
  195.                         src_offset*=10L;
  196.                         src_offset+=(long)(a1-'0');
  197.                      }
  198.                     ltoa(src_offset,temp,10);
  199.                     if(strcmp(temp,ent[cm]+2))
  200.                      {
  201.                       o_error:
  202.                         printf ("\nERROR: Invalid offset value. Expected => (/O0 - /O2147483647)%s",errormsg);
  203.                         return;
  204.                      }
  205.                     break;
  206.                 case 'S':
  207.                     a0=strlen(ent[cm]);
  208.                     if (a0>2)
  209.                      {
  210.                         if (asctoi(&startpos,ent[cm]+2,16))
  211.                             goto s_error;
  212.                         if (startpos&0xFF00)
  213.                             goto s_error;
  214.                      }
  215.                      else
  216.                      {
  217.                       s_error:
  218.                         printf ("\nERROR: Invalid value for the index of the first character to be loaded.\nExpected => (/S0 - /SFF)%s",errormsg);
  219.                         return;
  220.                      }
  221.                     break;
  222.                 case 'E':
  223.                     a0=strlen(ent[cm]);
  224.                     if (a0>2)
  225.                      {
  226.                         if (asctoi(&endpos,ent[cm]+2,16))
  227.                             goto e_error;
  228.                         if (endpos&0xFF00)
  229.                             goto e_error;
  230.                      }
  231.                      else
  232.                      {
  233.                       e_error:
  234.                         printf ("\nERROR: Invalid value for the index of the last character to be loaded.\nExpected => (/E0 - /EFF)%s",errormsg);
  235.                         return;
  236.                      }
  237.                     break;
  238.  
  239.                 hlp:
  240.                 case '?':
  241.                     printf ("\
  242.  * * * FREEWARE VERSION - MAY BE DISTRIBUTED FREELY * * *\n\
  243. Syntax: FONTSEL [option [option...]] fontname[.fnt] [option [option...]]\n\n\
  244. fontname is the file containing the DOS-FONT in raw format (no control data)\n\
  245. \t(default extension => .fnt)\n\
  246. options:\n\
  247. /I\t   => selects the negative of the font\n\
  248. /8\t   => box width 8\n\
  249. /9\t   => box width 9\n\
  250. /D or /A   => displays the font's character set\n\
  251. /R\t * => recalculates the box height\n\
  252. /M??\t   => selects a video mode (must be in hexadecimal notation)\n\
  253. /G\t   => use when graphics mode is selected\n\
  254. /L???\t   => selects number of scrool lines (valid => 0-255)\n\
  255. /H???\t   => font character height (default=16 valid => 0-255)\n\
  256. /O???...   => load font file from offset (default=0 valid => 0-2147483647)\n\
  257. /S??\t * => first character to be loaded (valid => 0-FF (in hexadecimal))\n\
  258. /E??\t * => last character to be loaded (valid => 0-FF (in hexadecimal))\n\
  259. /?\t   => this help screen\n\n\
  260. \t\t * => disabled when option /G is selected\n");
  261.                     return;
  262.                 default:
  263.                     printf ("\nERROR: Unknow option%s",errormsg);
  264.                     return;
  265.              }
  266.          }
  267.          else
  268.          {
  269.             if (progflag&0x01)
  270.              {
  271.                 printf ("\nERROR: Unexpected char '%c' in command line%s",*(ent[cm]),errormsg);
  272.                 return;
  273.              }
  274.             progflag|=0x01;
  275.             readfname (licos,ent[cm],".FNT"); //appends extension .FNT in file
  276.                               //name, if filename has no extension
  277.          }
  278.      }
  279.     if (!(progflag&0x01))
  280.      {
  281.         printf ("\nERROR: No font file supplied%s",errormsg);
  282.         return;
  283.      }
  284.  
  285.     buffer=(unsigned char*)malloc(256*changechar_height);
  286.     source=fopen (licos,"rb");
  287.     if(source==NULL)
  288.      {
  289.         printf ("\nERROR: FILE DOES NOT EXIST OR COULDN'T BE OPEN FOR READ%s",errormsg);
  290.         fcloseall();
  291.         return;
  292.      }
  293.     fseek(source,src_offset,0);
  294.     for (cont=0;cont<changechar_height*256;cont++)
  295.      {
  296.         if (!(flags&0x01))
  297.             buffer[cont]=(unsigned char)fgetc (source);
  298.          else
  299.             buffer[cont]=~(unsigned char)fgetc (source);
  300.      }
  301.     fclose (source);
  302.  
  303.     if (flags&0x20)
  304.      {
  305.         setcrtmode (3);
  306.         changechar_func=CHANGCHR_RECALC;
  307.      }
  308.  
  309.     if (flags&0x40)
  310.      {
  311.         setcrtmode (newmode);
  312.         crt_detect (VIDEOADAPTER);
  313.      }
  314.  
  315.     if (!(flags&0x80))
  316.      {
  317.         if (startpos>endpos)
  318.          {
  319.             printf ("\nERROR: First character to be loaded index is greater than\
  320.  last character to be\nloaded index.%s",errormsg);
  321.          }
  322.         changechar (buffer,startpos,(endpos-startpos)+1);
  323.      }
  324.      else
  325.      {
  326.         crt_direct=1;
  327.         if (!(flags&0x100))
  328.             changechar_rows=vmode_y;
  329.         goto jmp_100h;
  330.      }
  331.  
  332.     if (flags&0x100)
  333.       jmp_100h:
  334.         changecharg(buffer,changechar_rows);
  335.  
  336.     if (flags&0x08)
  337.         setchrboxwidth((flags&0x07)/2);
  338.  
  339.     if (flags&0x10)
  340.      {
  341.         crt_detect(VIDEOADAPTER);
  342.         fillscr (0x20,0x1e);
  343.         for (c0=0;c0<8;c0++)
  344.             for (c1=0;c1<16;c1++)
  345.              {
  346.                 printc (c0*16+c1,c1+17,c0+8,0x17);
  347.                 printc (c0*16+c1+128,c1+42,c0+8,0x17);
  348.              }
  349.         prints ("FONT CHARACTER SET:",25,3,0x1e);
  350.         for (c0=0;c0<8;c0++)
  351.          {
  352.             prints ("(00)h ",10,8+c0,0x1f);
  353.             prints ("(80)h ",60,8+c0,0x1f);
  354.             printc (c0+0x30,11,8+c0,0x1f);
  355.             printc (c0+0x5f,61,8+c0,0x1f);
  356.             printc ('║',37,8+c0,0x1f);
  357.          }
  358.         printc ('8',61,8,0x1f);
  359.         printc ('9',61,9,0x1f);
  360.         for (c0=0;c0<16;c0++)
  361.          {
  362.             printc ('0',c0+17,5,0x1f);
  363.             printc ('0',c0+42,5,0x1f);
  364.             if (c0<10)
  365.                 a0=c0+'0';
  366.              else
  367.                 a0=c0+0x57;
  368.             printc (a0,c0+17,6,0x1f);
  369.             printc (a0,c0+42,6,0x1f);
  370.          }
  371.         crt_gotoxy(0,16);
  372.      }
  373.     free(buffer);
  374.  }
  375. //0123456789Ωδ∞φε∩